{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "import os\n",
    "import random\n",
    "import time\n",
    "\n",
    "import numpy as np\n",
    "\n",
    "import swanlab\n",
    "from tutils import TEMP_PATH\n",
    "\n",
    "logdir = os.path.join(TEMP_PATH, \"sync\")"
   ],
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "run = swanlab.init(logdir=logdir, reinit=True, mode=\"offline\", project=\"test-sync\", description=\"test-sync\", experiment_name=\"sync\")\n",
    "epochs = 500\n",
    "lr = 0.01\n",
    "offset = random.random() / 5\n",
    "\n",
    "swanlab.config.epochs = epochs\n",
    "swanlab.config.learning_rate = lr\n",
    "swanlab.config.offset = offset\n",
    "\n",
    "# 模拟训练\n",
    "for epoch in range(2, swanlab.config.epochs):\n",
    "    acc = 1 - 2**-epoch - random.random() / epoch - offset\n",
    "    loss = 2**-epoch + random.random() / epoch + offset\n",
    "    print(f\"epoch={epoch}, accuracy={acc}, loss={loss}\")\n",
    "    if epoch % 10 == 0:\n",
    "        # 测试image\n",
    "        images = [swanlab.Image(np.random.randint(0, 255, (100, 100, 3)), caption=\"test\") for _ in range(epoch // 10)]\n",
    "        swanlab.log({\"test/image\": images}, step=epoch)\n",
    "        # 测试折线图\n",
    "        swanlab.log({\"t/accuracy\": acc, \"loss\": loss})\n",
    "    else:\n",
    "        # 测试折线图\n",
    "        swanlab.log({\"t/accuracy\": acc, \"loss\": loss})\n",
    "    time.sleep(1)"
   ],
   "id": "526d9f0c515586d9",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": "swanlab.finish()",
   "id": "1334a1b82f6e2b3f",
   "outputs": [],
   "execution_count": null
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
